package com.bilin.coin.activity;

import android.text.TextUtils;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bilin.coin.R;
import com.bilin.coin.bean.DigPayTypeBean;
import com.bilin.coin.common.base.BaseAppCompatActivity;
import com.bilin.coin.network.RetrofitConfig;
import com.bilin.coin.tools.Geter;
import com.bilin.coin.tools.Poster;
import com.bilin.coin.utils.GsonUtil;
import com.bilin.coin.widget.MyRadioGroup;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import butterknife.BindView;
import butterknife.OnClick;

@Route(path = "/safe/DigEnterOrderActivity")
public class DigEnterOrderActivity extends BaseAppCompatActivity {

    private String payWay = "";// 支付方式 0:100%WIC | 1: 50%WIC+50%WGP | 2:100%USDT | 3: 100% WGP

    private String goodsType = "";//购买的设备类型 1：推车|2：小挖机|3：运煤车|4：大挖机

    @BindView(R.id.rg_tools)
    MyRadioGroup rg_tools;
    @BindView(R.id.rg_coin)
    RadioGroup rg_coin;
    @BindView(R.id.coin_wic)
    RadioButton coin_wic;
    @BindView(R.id.coin_usdt)
    RadioButton coin_usdt;
    @BindView(R.id.coin_wgp)
    RadioButton coin_wgp;
    @BindView(R.id.coin_wgp_wic)
    RadioButton coin_wgp_wic;

    @BindView(R.id.tool11)
    RadioButton tool11;
    @BindView(R.id.tool22)
    RadioButton tool22;
    @BindView(R.id.tool33)
    RadioButton tool33;
    @BindView(R.id.tool44)
    RadioButton tool44;

    @BindView(R.id.tv_plan_put_in)
    TextView tv_plan_put_in;

    HashMap<String, String> hashMapType = new HashMap();
    private List<DigPayTypeBean> digPayTypeBeans;


    private void initEvent() {
        rg_tools.setOnCheckedChangeListener(new MyRadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(MyRadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.tool11:
                        goodsType = "1";
                        break;
                    case R.id.tool22:
                        goodsType = "2";
                        break;
                    case R.id.tool33:
                        goodsType = "3";
                        break;
                    case R.id.tool44:
                        goodsType = "4";
                        break;
                }
                fillData();
                getCost();
            }
        });
        rg_coin.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.coin_wic:
                        if (coin_wic.isChecked()) {
                            payWay = "0";
                        }
                        break;
                    case R.id.coin_usdt:
                        if (coin_usdt.isChecked()) {
                            payWay = "2";
                        }
                        break;
                    case R.id.coin_wgp:
                        if (coin_wgp.isChecked()) {
                            payWay = "3";
                        }
                        break;
                    case R.id.coin_wgp_wic:
                        if (coin_wgp_wic.isChecked()) {
                            payWay = "1";
                        }
                        break;
                }
                getCost();
            }
        });
    }

    private void getCost() {
        new Geter(this, false, true) {
            @Override
            protected String fillUrl() {
                return "/api/buy-log/getCost";
            }

            @Override
            protected Map<String, String> fillParams() {
                HashMap hashMap = new HashMap();
                hashMap.put("goodsType",goodsType);
                hashMap.put("payWay",payWay);
                return hashMap;
            }

            @Override
            protected void disposeSuccess(String dataJson) {
                super.disposeSuccess(dataJson);
                amount = dataJson;
                tv_plan_put_in.setText("预计需投入："+dataJson);
            }
        };
    }
    String amount = "";
    @Override
    protected void initView() {
        setTitle("挖矿入单");
        setMenu("入单规则");
        initEvent();
        getData();
    }

    @Override
    public void setMenuOnClickListener() {
        super.setMenuOnClickListener();
        baseStartActivityWith("/common/WebPageLoading")
                .withString("url", RetrofitConfig.newsUrl +"14")
                .withString("title", "入单规则")
                .navigation();
    }

    private void getData() {

        new Geter(this, false, true) {
            @Override
            protected String fillUrl() {
                return "/api/buy-log/getPayWay";
            }

            @Override
            protected Map<String, String> fillParams() {
                return new HashMap<>();
            }

            @Override
            protected void disposeSuccess(String dataJson) {
                super.disposeSuccess(dataJson);
                digPayTypeBeans = GsonUtil.jsonToList(dataJson, DigPayTypeBean.class);
                for (DigPayTypeBean digPayTypeBean : digPayTypeBeans) {
                    String payWay = digPayTypeBean.getPayWay();
                    if (!TextUtils.isEmpty(payWay)) {
                        hashMapType.put(digPayTypeBean.getType(), payWay);
                    }
                }
                //显示设备
                Set<String> keySet = hashMapType.keySet();
                tool11.setVisibility(keySet.contains("1")?View.VISIBLE:View.GONE);
                tool22.setVisibility(keySet.contains("2")?View.VISIBLE:View.GONE);
                tool33.setVisibility(keySet.contains("3")?View.VISIBLE:View.GONE);
                tool44.setVisibility(keySet.contains("4")?View.VISIBLE:View.GONE);
                fillData();
            }
        };
    }

    private void fillData() {
        if (digPayTypeBeans == null) return;

        if (TextUtils.isEmpty(goodsType)) {
            //goodsType = digPayTypeBeans.get(0).getType();//默认第一个
            goodsType = new ArrayList<>(hashMapType.keySet()).get(0);//默认第一个
        }

        String payWays = hashMapType.get(goodsType);//购买的设备类型 1：推车|2：小挖机|3：运煤车|4：大挖机
      /*  if (TextUtils.isEmpty(payWay)) {
            //默认选中第一个
            payWay = payWays.substring(0, 1);
        } else {//设备变了
            payWay = payWays.substring(0, 1);
        }*/
        if (!TextUtils.isEmpty(payWays)) {
            payWay = payWays.substring(0, 1);
        }

        //显示购买方式 支付方式 0:100%WIC | 1: 50%WIC+50%WGP | 2:100%USDT | 3: 100% WGP
        coin_wic.setVisibility(payWays.contains("0") ? View.VISIBLE : View.GONE);
        coin_wgp_wic.setVisibility(payWays.contains("1") ? View.VISIBLE : View.GONE);
        coin_usdt.setVisibility(payWays.contains("2") ? View.VISIBLE : View.GONE);
        coin_wgp.setVisibility(payWays.contains("3") ? View.VISIBLE : View.GONE);

        tool11.setChecked(goodsType.equals("1"));
        tool22.setChecked(goodsType.equals("2"));
        tool33.setChecked(goodsType.equals("3"));
        tool44.setChecked(goodsType.equals("4"));

        coin_wic.setChecked(payWay.equals("0"));
        coin_wgp_wic.setChecked(payWay.equals("1"));
        coin_usdt.setChecked(payWay.equals("2"));
        coin_wgp.setChecked(payWay.equals("3"));


    }

    @Override
    protected void initData() {

    }

    @OnClick({R.id.bt_confirm,R.id.tv_buy_record})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_confirm:
                confirm();
                break;
            case R.id.tv_buy_record:
                baseStartActivity("/mall/BuyRecordListActivity");
                break;

        }
    }

    private void confirm() {
    /*    if (TextUtils.isEmpty(amount)) {
            showToast("正在获取预计投入数量");
            return;
        }*/
        new Poster(this, true, true) {
            @Override
            protected String fillUrl() {
                return "/api/buy-log/buy";
            }

            @Override
            protected Map<String, String> fillParams() {
                HashMap hashMap = new HashMap();
               // hashMap.put("amount", amount);
                hashMap.put("goodsType", goodsType);
                hashMap.put("payWay", payWay);
                return hashMap;
            }

            @Override
            protected void disposeSuccess(String dataJson) {
                super.disposeSuccess(dataJson);
                finish();
            }
        };
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_dig_enter_order;
    }
}
